/* Task板块的Store */
import { makeAutoObservable, runInAction } from 'mobx'
import { getTaskList } from '../api'

export default class TaskStore {
    constructor(root) { 
        // root:最外层Store类的实例[包含各个版本Store的实例]
        // 我们以后可以在Task板块中，基于this.root获取根Store实例，基于根Store实例，访问其它板块的Store实例
        this.root = root
        makeAutoObservable(this);
    }

    taskList = null;
    /* 异步获取全局任务 */
    queryAllTaskAction = async ()=>{
        let list = [];
        try {
            let result = await getTaskList(0);
            if (+result.code === 0) {
                list = result.list
            }
        } catch (_) { }
        runInAction(() => {
            this.taskList = list
        });
    }

    /* 同步删除某一任务 */
    removeTaskAction = (id)=> {
        let { taskList } = this;
        if (!Array.isArray(taskList)) return;
        this.taskList = taskList.filter(item => {
            return +item.id !== +id
        })
    }

    /* 同步修改某一任务 */
    updateTaskAction = (id)=> {
        let { taskList } = this;
        if (!Array.isArray(taskList)) return;
        this.taskList = taskList.map(item => {
            if (+item.id === +id) {
                item.state = 2;
                item.complete = new Date().toLocaleString()
            }
            return item
        })
    }
}